package com.jnu.testdrivendevelopment.data;

import android.content.Context;

import com.jnu.testdrivendevelopment.R;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class DataBank {
    public static final String DATA_NAME = "data";
    private final Context context;
    private List<Book> bookArrayList=new ArrayList<>();

    public DataBank(Context context) {
        this.context=context;
    }

    public List<Book> loadData() {

        ObjectInputStream ois=null;
        try{
            ois = new ObjectInputStream(context.openFileInput(DATA_NAME));
            bookArrayList=(ArrayList<Book>) ois.readObject();
        }catch(Exception e){
            e.printStackTrace();
        }
        return bookArrayList;
    }

    public void saveData() {
        ObjectOutputStream oos=null;
        try{
            oos = new ObjectOutputStream(context.openFileOutput(DATA_NAME,Context.MODE_PRIVATE));
            oos.writeObject(bookArrayList);
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
